package com.shqcjd.yunweiyanoperator.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.eventbus.CompanyRegisterSuccessEvebt;
import com.shqcjd.yunweiyanoperator.model.pojo.CompanyBean;
import com.shqcjd.yunweiyanoperator.ui.adapter.CompanyAdapter;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class CompanyActivity extends BaseActivity {

    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_company)
    EditText etCompany;
    @BindView(R.id.rv_company)
    RecyclerView rvCompany;
    TextView tvCreateCompany;


    private Context mContext;
    private CompanyAdapter companyAdapter;
    List<CompanyBean> mList = new ArrayList<>();
    private View notDataView;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_company;
    }

    @Override
    public void init() {
        super.init();
        mContext = this;
    }

    @Override
    public void initView() {
        super.initView();
        EventBus.getDefault().register(this);
        tvTitle.setText("选择企业");
        companyAdapter = new CompanyAdapter(mList);
        rvCompany.setLayoutManager(new LinearLayoutManager(mContext));
        rvCompany.setAdapter(companyAdapter);
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvCompany.getParent(), false);
        tvCreateCompany = notDataView.findViewById(R.id.tv_create_company);
        tvCreateCompany.setVisibility(View.VISIBLE);
        companyAdapter.setEmptyView(notDataView);
        etCompany.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().equals("")){
                    if(companyAdapter!=null){
                        mList.clear();
                        companyAdapter.notifyDataSetChanged();
                        companyAdapter.setEmptyView(notDataView);
                    }
                }else{
                    queryCompany(s.toString());
                }
            }
        });

        tvCreateCompany.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(mContext, CompanyRegisterActivity.class);
                startActivity(it);
                finish();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFinishCompanyRegisterEvent(CompanyRegisterSuccessEvebt event) {
        finish();
    }

    /**
     * 查詢公司
     */
    public void queryCompany(String str) {
        ApiService.queryCompany(str)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<List<CompanyBean>>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
//                        showWaitingDialog("正在加载...");
                    }


                    @Override
                    public void _onNext(ResponseData<List<CompanyBean>> listResponseData) {
                        if(listResponseData.retCode==10000){
                            mList.clear();
                            mList.addAll(listResponseData.data);
                            companyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    Intent it = new Intent();
                                    it.putExtra("name", mList.get(position).name);
                                    it.putExtra("companyId", mList.get(position).id+"");
                                    setResult(110, it);
                                    finish();
                                }
                            });
                            companyAdapter.notifyDataSetChanged();
                        }else{
                            ToastUtils.showShort(mContext, listResponseData.retMsg);
                        }

                    }

                    @Override
                    public void _onError(String errorMessage) {

                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }

                });
    }

    @OnClick(R.id.btn_finish)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
